## Introduction
This repository is intended for replication of the numerical results appearing in the main text and online appendices of the paper "Robust Estimation and Inference in Panels with Interactive Fixed Effects," authored by [Timothy B. Armstrong](https://tbarmstr.github.io/), [Martin Weidner](https://users.ox.ac.uk/~econ0610/), and [Andrei Zeleneev](https://www.azeleneev.com/), and accepted for publication in the [*Journal of Political Economy*](https://www.journals.uchicago.edu/toc/jpe/current). ([arXiv version](https://arxiv.org/abs/2210.06639))

## Reproducibility Workflow and List of Tables/Figures

All simulations were conducted in `Matlab` (version `R2025a`).

For the Monte Carlo simulation, we performed $5000$ replications and utilized $8$ cores for parallel computation. These settings can be adjusted based on one's computational resources.

This replication package will go through the following figures and tables in the paper:

- An illustrative example from a subset of the results in the Monte Carlo study.
  - Figure 1: Finite sample distributions of the LS and the debiased estimators, $N = 100$, $T = 50$, $R = 1$ *(produced by the main script `run_mc_R01_DGP01.m`)*
- Results for the simulation study in Section 5.1 in the main text
  - Table 1: Simulation results for the experiment in Section 5.1, $N = 100$, $R = 1$ *(produced by the main script `run_mc_R01_DGP01.m`)*
  - Table 2: Simulation results for the experiment in Section 5.1, $N = 100$, $T = 50$, $R = 2$ *(produced by the main script `run_mc_R02_DGP01.m`)*
  - Table 3: Simulation results for the experiment in Section 5.1, $N = 100$, $T = 50$, $R = 2$ *(produced by the main script `run_mc_R02_DGP01.m`)*
- Results for the empirical illustration in Section 5.2 in the main text
  - Table 4: Simulation results for the empirically calibrated experiment, $N = 48$, $T = 33$, $R = 1$ *(produced by the main script `run_mc_empirical.m`)*
  - Table 5: LS and debiased estimates and 95% CIs for $\beta$ *(produced by the main script `run_empirical_1.m`)*
- Additional results for the simulation study in Appendix C
  - Table 6: Simulation results for the experiment in Section 5.1, $N = 50$, $R = 1$ *(produced by the main script `run_mc_R01_DGP01.m`)*
  - Table 7: Simulation results for the experiment in Section 5.1, $N = 300$, $R = 1$ *(produced by the main script `run_mc_R01_DGP01.m`)*
  - Table 8: Simulation results for the experiment in Section C.2 *(produced by the main script `run_mc_R01_DGP02.m`)*
  - Table 9: Simulation results for the non-robust CI (13) *(produced by the main script `run_mc_R01_DGP01.m`)*
- Additional results for the empirical illustration in Appendix D
  - Table 10: LS and debiased estimates and 95% CIs for dynamic effects of divorce law reform *(produced by the main script `run_empirical_4.m`)*

To reproduce the desired figures or tables, please run the corresponding main script.

## Main Scripts, Functions, Data, and Folders

We document all the Matlab `.m` files and their usage in this section. These Matlab `.m` files can be further distinguished into the main scripts, which will output the desired tables and figures, and the auxiliary functions, which are the underlying functions and algorithms.

### Main scripts

- `run_mc_R01_DGP01.m`: This script produces the results of a Monte Carlo simulation, where the DGP is set as a simple static factor model and the number of factors $R$ being $1$, with various settings for the number of units ($N$) and the number of time periods ($T$). It also outputs Table 1 in Section 5.1, Tables 6, 7, and 9 in Appendix C, and Figure 1 in Section 1.
- `run_mc_R02_DGP01.m`: This script produces the results of a Monte Carlo simulation, where the DGP is set as a simple static factor model and the number of factors $R$ being $2$, with various settings for the number of units ($N$) and the number of time periods ($T$). It also outputs Tables 2 and 3 in Section 5.1.

- `run_mc_empirical.m`: This script produces the results of a numerical experiment calibrated to imitate an actual empirical setting, where the experiment is based on the seminal studies of the effects of unilateral divorce law reforms on the US divorce rates by [Friedberg (1998)](https://www.jstor.org/stable/116852)[^3] and [Wolfers (2006)](http://doi.org/10.1257/aer.96.5.1802)[^11], subsequently revisited by [Kim and Oka (2014)](https://doi.org/10.1002/jae.2310)[^6] and [Moon and Weidner (2015)](https://doi.org/10.3982/ECTA9382)[^8] in the context of interactive fixed effects models. It also outputs Table 4 in Section 5.2.
- `run_empirical_1.m`: This script produces the estimation and inference results for the actual data set with the bi-annual dummies as in [Wolfers (2006)](http://doi.org/10.1257/aer.96.5.1802)[^11], where the experiment is based on the seminal studies of the effects of unilateral divorce law reforms on the US divorce rates by [Friedberg (1998)](https://www.jstor.org/stable/116852)[^3] and [Wolfers (2006)](http://doi.org/10.1257/aer.96.5.1802)[^11], subsequently revisited by [Kim and Oka (2014)](https://doi.org/10.1002/jae.2310)[^6] and [Moon and Weidner (2015)](https://doi.org/10.3982/ECTA9382)[^8] in the context of interactive fixed effects models. It also outputs Table 5 in Section 5.2.
- `run_empirical_4.m`: This script produces the estimation and inference results for the actual data set with coarser dynamics of treatment effects, where the experiment is based on the seminal studies of the effects of unilateral divorce law reforms on the US divorce rates by [Friedberg (1998)](https://www.jstor.org/stable/116852)[^3] and [Wolfers (2006)](http://doi.org/10.1257/aer.96.5.1802)[^11], subsequently revisited by [Kim and Oka (2014)](https://doi.org/10.1002/jae.2310)[^6] and [Moon and Weidner (2015)](https://doi.org/10.3982/ECTA9382)[^8] in the context of interactive fixed effects models. It also outputs Table 10 in Appendix D.
- `run_mc_R01_DGP02.m`: This script produces the results of a Monte Carlo simulation, where the DGP is set as a design with additional covariate and heteroskedastic serially correlated errors. It also outputs Table 8 in Appendix C.

### Auxiliary functions

- `gen_stats.m`: This function computes statistics for simulation results.
- `get_default_field.m`: Function to retrieve the value from a structure, with a default value if the field is missing or the structure is empty.
- `honest_weak_factors.m`: This function implements the main bias-aware method that considers the case when some of the factors are weak.
- `hybrid_fmincon2.m`: This function performs a two-stage optimization procedure to minimize the objective function with nonlinear constraints, where it uses a global method (e.g., pswarm) in the first stage and local refinement (fmincon) in the second stage.
- `LS_factor.m`: Code by Martin Weidner for least squares estimation of linear panel regression models with interactive fixed effects. This code was used to implement the estimator in the papers *Linear Regression for Panel with Unknown Number of Factors as Interactive Fixed Effects*, [Moon and Weidner (2015)](https://doi.org/10.3982/ECTA9382)[^8], and *Dynamic Linear Panel Regression Models with Interactive Fixed Effects*, [Moon and Weidner (2017)](https://doi.org/10.1017/S0266466615000328)[^9].
- `LS_factor2.m`: This code further modifies the original `LS_factor.m` by allowing additional input parameters for known factors and factor loadings.
- `mc_weak_factors.m`: This is the main MC function that produces the results for a single design. It computes and saves the relevant statistics for the standard LS approach and for the proposed method.
- `PSwarm1.m`: The particle swarm pattern search algorithm for global optimization originally by A. Ismael F. Vaz and L. N. Vicente.

### Data

The data we use, `DivorceRate1956_1988.csv`, is downloaded from the replication package [Moon and Weidner (2015)](https://doi.org/10.3982/ECTA9382)[^8] at [https://doi.org/10.3982/ECTA9382](https://doi.org/10.3982/ECTA9382). This is the divorce rate data in the USA from 1956 to 1988, originally from [Friedberg (1998)](https://www.jstor.org/stable/116852)[^3] and [Wolfers (2006)](http://doi.org/10.1257/aer.96.5.1802)[^11]. Here, this empirical dataset follows [Kim and Oka (2014)](https://doi.org/10.1002/jae.2310)[^6] and uses their procedure to construct a balanced panel with $N = 48$ states and $T = 33$ years.

This data contains 1,650 rows and 88 variables, where each column represents:

- `st`: Two-letter state code.
- `id_st`: Numeric ID for the states.
- `year`: Year of observation.
- `div_rate_rev01`: Divorces per 1000 people, 1956-1998.
- `div_rate_rev02`: Divorces per 1000 people, 1956-1998.
- `stpop`: State population.
- `unilateral`: Dummy for unilateral law in [Friedberg (1998)](https://www.jstor.org/stable/116852)[^3].
- `dyn_uni2` to `dyn_uni9`: Dynamic treatment effects (years 1-2, 3-4, 5-6, 7-8, 9-10, 11-12, 13-14, 15+) dummy for the year of unilateral law in [Friedberg (1998)](https://www.jstor.org/stable/116852)[^3].
- `dyn_gruber_2` to `dyn_gruber_9`: Dynamic treatment effects (years 1-2, 3-4, 5-6, 7-8, 9-10, 11-12, 13-14, 15+) dummy for the year of divorce law reform according to [Gruber (2004)](https://doi.org/10.1086/423155)[^4].
- `dyn_johnson_2` to `dyn_johnson_9`: Dynamic treatment effects (years 1-2, 3-4, 5-6, 7-8, 9-10, 11-12, 13-14, 15+) dummy for the year of divorce law reform according to [Johnson and Mazingo (2000)](https://ssrn.com/abstract=236227)[^5].
- `dyn_mechoulan_2` to `dyn_mechoulan_9`: Dynamic treatment effects (years 1-2, 3-4, 5-6, 7-8, 9-10, 11-12, 13-14, 15+) dummy for the year of divorce law reform according to [Mechoulan (2006)](https://doi.org/10.1086/498832)[^7].
- `dyn_ellmanlohr1_2` to `dyn_ellmanlohr1_9`: Dynamic treatment effects (years 1-2, 3-4, 5-6, 7-8, 9-10, 11-12, 13-14, 15+) dummy for the year of divorce law reform according to [Ellman and Lohr (1998)](https://doi.org/10.1016/S0144-8188(98)00014-3)[^2] (definition a).
- `dyn_ellmanlohr2_2` to `dyn_ellmanlohr2_9`: Dynamic treatment effects (years 1-2, 3-4, 5-6, 7-8, 9-10, 11-12, 13-14, 15+) dummy for the year of divorce law reform according to [Ellman and Lohr (1998)](https://doi.org/10.1016/S0144-8188(98)00014-3)[^2] (definition b).
- `dyn_brinigbuckley_2` to `dyn_brinigbuckley_9`: Dynamic treatment effects (years 1-2, 3-4, 5-6, 7-8, 9-10, 11-12, 13-14, 15+) dummy for the year of divorce law reform according to [Brinig and Buckley (1998)](https://doi.org/10.1016/S0144-8188(98)00008-8)[^1].
- `dyn_nakonezny_2` to `dyn_nakonezny_9`: Dynamic treatment effects (years 1-2, 3-4, 5-6, 7-8, 9-10, 11-12, 13-14, 15+) dummy for the year of divorce law reform according to [Nakonezny, Shull and Rodgers (1995)](https://doi.org/10.2307/353580)[^10].
- `divx1` to `divx17`: [Friedberg (1998)](https://www.jstor.org/stable/116852)[^3]'s dummies for coding breaks; see appendix of the paper.

### Folders

Under the main directory and after running the main scripts, there will be three more folders to store the results:

- `/matlogs`: Subdirectory that stores all simulation replications across different settings in `.mat` format, where
  - `/matlogs/R01_DGP01` contains results where the number of factors $R$ is set to $1$ and the data generating process is a simple factor model,
  - `/matlogs/R02_DGP01` contains results where the number of factors $R$ is set to $2$ and the data generating process is a simple factor model,
  - `/matlogs/R01_DGP02` contains results where the number of factors $R$ is set to $1$ and the data generating process follows the design with additional covariate and heteroskedastic serially correlated errors.
- `/tables`: Subdirectory that stores the results for Tables 1 to 10 in `.csv` format.
- `/figures`: Subdirectory that stores the results for Figure 1 in `.pdf` format.

Additionally, these results generated for the paper are included in the replication package within the files `matlogs.zip`, `tables.zip`, and `figures.zip`.

## Monte Carlo Simulations

In this section, we will go through the details of each Monte Carlo simulation.

### Section 5.1 and Appendix C.1: $R=1$ under simple factor model DGP
Run the main script `run_mc_R01_DGP01.m` for this simulation. It produces the following results:

- `/matlogs/R01_DGP01/mc_DGP1_***.mat`: These files are named with respect to the parameter settings. For example, the file `mc_DGP1_N50_T300_R01_kappa010_reps5000.mat` store the result of $N=50$, $T = 300$, $R = 1$, $\kappa = 0.1$ for $5000$ replications.
- `/matlogs/R01_DGP01/MC_merged_w_LFCV.mat`: This file stores the merged result after calculating the oracle confidence intervals that are based on unknown design-specific least favorable critical values.
- `/tables/table1.csv`, `/tables/table6.csv`, `/tables/table7.csv`, and `/tables/table9.csv`: Results for Tables 1, 6, 7, and 9 in `.csv` format.

- `/figures/N100_T50_R1_kappa***.pdf`: Results for Figure 1 in `.pdf` format, where `000, 010, 020, 100` in the file names represent $\kappa = 0, 0.1, 0.2, 1$ respectively.

### Section 5.1: $R=2$ under simple factor model DGP

Run the main script `run_mc_R02_DGP01.m` for this simulation. It produces the following results:

- `/matlogs/R02_DGP01/mc_DGP1_***.mat`: These files are named with respect to the parameter settings. For example, the file `mc_DGP1_N100_T50_R02_kappa020_015_reps5000.mat` store the result of $N=100$, $T = 50$, $R = 2$, $\kappa_1 = 0.2$, $\kappa_2 = 0.15$ for $5000$ replications.
- `/matlogs/R02_DGP01/MC_merged_w_LFCV.mat`: This file stores the merged result after calculating the oracle confidence intervals that are based on unknown design-specific least favorable critical values.
- `/tables/table2.csv` and `/tables/table3.csv`: Results for Tables 2 and 3 in `.csv` format.

### Section 5.2: empirical calibrated DGP

Run the main script `run_mc_empirical.m` for this simulation. It produces the following results:

- `/matlogs/mc_empirical_N48_T33_R01_reps5000.mat`: This file stores the simulation results under empirically calibrated balanced panel with $N = 48$ states, $T = 33$ years, $R=1$ for $5000$ replications.
- `/tables/table4.csv`: Results for Table 4 in `.csv` format.

### Appendix C.2: DGP with additional covariate and heteroskedastic serially correlated errors

Run the main script `run_mc_R01_DGP02.m` for this simulation. It produces the following results:

- `/matlogs/R01_DGP02/mc_DGP2_***.mat`: These files are named with respect to the parameter settings. For example, the file `mc_DGP2_N100_T50_R01_kappa015_reps5000.mat` store the result of $N=100$, $T = 50$, $R = 1$, $\kappa = 0.15$ for $5000$ replications.
- `/matlogs/R02_DGP01/MC_merged_w_LFCV.mat`: This file stores the merged result after calculating the oracle confidence intervals that are based on unknown design-specific least favorable critical values.
- `/tables/table8.csv`: Results for Table 8 in `.csv` format.

### Scope and Runtimes

All simulation-related main scripts were run on a Linux machine with an 8-core/16-thread CPU and 32 GB of RAM. Most main scripts for the simulation can be completed in approximately 4 hours, whereas the `run_mc_R01_DGP02.m` with additional covariates will need 8-12 hours depending on the setting of parallel computation and CPU specs.

## Empirical Illustration

Run the main script `run_empirical_1.m` and `run_empirical_4.m` for the empirical illustration. It produces the following results:

- `/tables/table5.csv` and `/tables/table10.csv`: Results for Tables 5 and 10 in `.csv` format.

These two main scripts for empirical illustration can be run on a standard desktop machine in less than 1-2 minutes.

## Licensing Notes

This replication package is released under the MIT License (see `LICENSE` file).

### Third-party code
- The file `PSwarm1.m` is based and modified from `PSwarm.m` by A. Ismael F. Vaz and L. N. Vicente. The latter was originally distributed under the GNU Lesser General Public License, see `PSwarm1.m` for more details. For redistribution and reuse terms, please refer to the original licensing information.

### Empirical dataset
- The file `DivorceRate1956_1988.csv` is obtained from the replication package of Moon and Weidner (2015) [https://doi.org/10.3982/ECTA9382]. It is included here for academic replication use only, following the terms under which it was originally made publicly available.

## References
- [^1]: Brinig, M. F., & Buckley, F. H. (1998). No-fault laws and at-fault people. *International Review of Law and Economics*, *18*(3), 325-340.
- [^2]: Ellman, I. M., & Lohr, S. L. (1998). Dissolving the relationship between divorce laws and divorce rates. *International Review of Law and Economics*, *18*(3), 341-359.
- [^3]: Friedberg, L. (1998). Did Unilateral Divorce Raise Divorce Rates? Evidence from Panel Data. The American Economic Review, 88(3), 608-627.
- [^4]: Gruber, J. (2004). Is making divorce easier bad for children? The long-run implications of unilateral divorce. *Journal of Labor Economics*, *22*(4), 799-833.
- [^5]: Johnson, J. H., & Mazingo, C. J. (2000). The economic consequences of unilateral divorce for children. *University of Illinois CBA Office of Research Working Paper 00-0112*.
- [^6]: Kim, D., & Oka, T. (2014). Divorce law reforms and divorce rates in the USA: an interactive fixed‐effects approach. *Journal of Applied Econometrics*, *29*(2), 231-245.
- [^7]: Mechoulan, S. (2006). Divorce laws and the structure of the American family. *The Journal of Legal Studies*, *35*(1), 143-174.
- [^8]: Moon, H. R., & Weidner, M. (2015). Linear regression for panel with unknown number of factors as interactive fixed effects. *Econometrica*, *83*(4), 1543-1579.
- [^9]: Moon, H. R., & Weidner, M. (2017). Dynamic linear panel regression models with interactive fixed effects. *Econometric Theory*, *33*(1), 158-195.
- [^10]: Rodgers, J. L., Nakonezny, P. A., & Shull, R. D. (1999). Did no-fault divorce legislation matter? Definitely yes and sometimes no. *Journal of Marriage and Family*, *61*(3), 803-809.
- [^11]: Wolfers, J. (2006). Did unilateral divorce laws raise divorce rates? A reconciliation and new results. American Economic Review, 96(5), 1802-1820.

